﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Susam
{
    public partial class PublicationMetadata : Form
    {
        private String URI;
        public PublicationMetadata(String bookURI)
        {
            this.URI = bookURI;
            InitializeComponent();
        }

        private void PublicationMetadata_Load(object sender, EventArgs e)
        {
            IFileParser parser = FileParserFactory.CreateParser(this.URI);
            Metadata meta = parser.Parse(this.URI);

            this.labelFilePath.Text = meta._file.filePath;
            this.labelFileType.Text = meta._file.fileType;
            this.labelTimestamp.Text = meta._file.timestamp.ToString();
            this.labelFileName.Text = meta._file.fileName;

            this.labelTitle.Text = meta.title;

            var rowCount = tableLayoutPanelPublicationMetadata.RowCount;

            foreach (var creator in meta.authorList)
            {
                var label = GetNewBoldLabel("Creator");
                var value = GetNewLabel(creator.name);

                tableLayoutPanelPublicationMetadata.Controls.Add(label, 0, rowCount);
                tableLayoutPanelPublicationMetadata.Controls.Add(value, 1, rowCount);
                rowCount = rowCount + 1;
            }

            foreach (var subject in meta.subjectList)
            {
                var label = GetNewBoldLabel("Subject");
                var value = GetNewLabel(subject);

                tableLayoutPanelPublicationMetadata.Controls.Add(label, 0, rowCount);
                tableLayoutPanelPublicationMetadata.Controls.Add(value, 1, rowCount);
                rowCount = rowCount + 1;
            }

            // Description

            foreach (var publisher in meta.publisherList)
            {
                var label = GetNewBoldLabel("Publisher");
                var value = GetNewLabel(publisher);

                tableLayoutPanelPublicationMetadata.Controls.Add(label, 0, rowCount);
                tableLayoutPanelPublicationMetadata.Controls.Add(value, 1, rowCount);
                rowCount = rowCount + 1;
            }

            foreach (var contributor in meta.contributorList)
            {
                var label = GetNewBoldLabel("Contributor");
                var value = GetNewLabel(contributor.name);

                tableLayoutPanelPublicationMetadata.Controls.Add(label, 0, rowCount);
                tableLayoutPanelPublicationMetadata.Controls.Add(value, 1, rowCount);
                rowCount = rowCount + 1;
            }

            foreach (var identifier in meta.identifierList)
            {
                var label = GetNewBoldLabel("Identifier");
                var value = GetNewLabel(identifier);

                tableLayoutPanelPublicationMetadata.Controls.Add(label, 0, rowCount);
                tableLayoutPanelPublicationMetadata.Controls.Add(value, 1, rowCount);
                rowCount = rowCount + 1;
            }

            // Source

            foreach (var lang in meta.languageList)
            {
                var label = GetNewBoldLabel("Language");
                var value = GetNewLabel(lang);

                tableLayoutPanelPublicationMetadata.Controls.Add(label, 0, rowCount);
                tableLayoutPanelPublicationMetadata.Controls.Add(value, 1, rowCount);
                rowCount = rowCount + 1;
            }

            // Empty Row
            var lastlabel = GetNewBoldLabel(" ");
            var lastvalue = GetNewLabel(" ");

            tableLayoutPanelPublicationMetadata.Controls.Add(lastlabel, 0, rowCount);
            tableLayoutPanelPublicationMetadata.Controls.Add(lastvalue, 1, rowCount);
            rowCount = rowCount + 1;


        }

        private Label GetNewLabel(String text)
        {
            var label = new Label();
            label.Text = text;
            label.Dock = System.Windows.Forms.DockStyle.Fill;
            label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

            return label;
        }

        private Label GetNewBoldLabel(String text)
        {
            var label = GetNewLabel(text);
            label.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            return label;
        }

    }
}
